package com.volunteer.controller;

import com.volunteer.entity.User;
import com.volunteer.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试控制器 - 用于调试
 */
@RestController
@RequestMapping("/api/test")
@RequiredArgsConstructor
public class TestController {

    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;

    /**
     * 测试数据库连接
     */
    @GetMapping("/db")
    public Map<String, Object> testDatabase() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> users = userMapper.selectList(null);
            result.put("success", true);
            result.put("userCount", users.size());
            result.put("users", users);
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        return result;
    }

    /**
     * 测试密码加密
     */
    @GetMapping("/password")
    public Map<String, Object> testPassword(@RequestParam(defaultValue = "123456") String password) {
        Map<String, Object> result = new HashMap<>();
        String encoded = passwordEncoder.encode(password);
        result.put("plainPassword", password);
        result.put("encodedPassword", encoded);
        result.put("matches", passwordEncoder.matches(password, encoded));
        
        // 测试与数据库中的密码是否匹配
        String dbPassword = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl.jZYW.Dlm";
        result.put("matchesDbPassword", passwordEncoder.matches(password, dbPassword));
        
        return result;
    }

    /**
     * 验证特定用户
     */
    @GetMapping("/user/{username}")
    public Map<String, Object> testUser(@PathVariable String username, 
                                        @RequestParam(defaultValue = "123456") String password) {
        Map<String, Object> result = new HashMap<>();
        try {
            User user = userMapper.selectOne(
                new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<User>()
                    .eq(User::getUsername, username)
            );
            
            if (user == null) {
                result.put("success", false);
                result.put("message", "用户不存在");
            } else {
                result.put("success", true);
                result.put("username", user.getUsername());
                result.put("nickname", user.getNickname());
                result.put("role", user.getRole());
                result.put("status", user.getStatus());
                result.put("passwordMatch", passwordEncoder.matches(password, user.getPassword()));
                result.put("passwordPrefix", user.getPassword().substring(0, 20));
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        return result;
    }
}

